'use strict';

app.controller('statCtrl', ['$scope','$http','loginService', function($scope,$http,loginService){
	 $scope.init = function() {
	    	 loginService.islogged();
			 $scope.link = loginService.getLink();			 
	    	 $scope.statistic_user();
	    	 $scope.statistic_group();
	    	 $scope.statistic_event();
			 $scope.statistic_interest();
	    };
	
	
	 $scope.statistic_user = function(){
			$http.get($scope.link+'users').success(function(data,status, config){
				console.log('load list of user for statistic');
				
			//	$scope.model_stat = data;
//				alert(JSON.stringify(data));
				$scope.count_nbr_user(data);
				
				//today(v,value);
			})
			.error(function(data, status, config) {
				console.log('can not load list of user for statistic');
		    }); 
	 }
	 
	 $scope.statistic_group = function(){
		 $http.get($scope.link+'groups').success(function(data,status, config){
				console.log('load list of groups for statistic');
			    $scope.nb_group = data.data.length;
			    $scope.count_nbr_group(data);
			})
			.error(function(data, status, config) {
				console.log('can not load list of groups for statistic');
		    });
	 }
	 
	 
	 $scope.statistic_event = function(){
		 $http.get($scope.link+'events').success(function(data,status, config){
				console.log('load list of events for statistic');
				$scope.nb_events = data.data.length;
			    $scope.count_nbr_event(data);
			})
			.error(function(data, status, config) {
				console.log('can not load list of events for statistic');
		    });
	 }
	 
	  $scope.statistic_interest = function(){
		 $http.get($scope.link+'lst_interest').success(function(data,status, config){
				console.log('load list of interests for statistic');
				$scope.nb_interests = data.data.length;
			})
			.error(function(data, status, config) {
				console.log('can not load list of interests for statistic');
		    });
	 }
	 
	 $scope.count_nbr_user = function (data) {
//			$scope.a = new Array();
			var i = 0;
			var nb_register_1_day = 0;
			var nb_register_1_week = 0;
			var nb_register_1_month = 0;
			
			angular.forEach(data.data,function(){
				if( compare(data.data[i].register_time,1) ) {
					nb_register_1_day ++;
				}
				if( compare(data.data[i].register_time,7) ) {
					nb_register_1_week ++;
				}
				if( compare(data.data[i].register_time,31) ) {
					nb_register_1_month ++;
				}

	            i++;
	        })
	        $scope.nbr_rows = i ;
			$scope.nb_register_1_day = nb_register_1_day ;
			$scope.nb_register_1_week = nb_register_1_week ;
			$scope.nb_register_1_month = nb_register_1_month ;
		}
	
	 
	 $scope.count_nbr_group = function (data) {
//			$scope.a = new Array();
			var i = 0;
			var nb_group_register_1_day = 0;
			var nb_group_register_1_week = 0;
			var nb_group_register_1_month = 0;
			
			angular.forEach(data.data,function(){
				if( compare(data.data[i].register_time,1) ) {
					nb_group_register_1_day ++;
				}
				if( compare(data.data[i].register_time,7) ) {
					nb_group_register_1_week ++;
				}
				if( compare(data.data[i].register_time,31) ) {
					nb_group_register_1_month ++;
				}
	            i++;
	        })
	     //   $scope.nbr_rows = i ;
			$scope.nb_group_register_1_day = nb_group_register_1_day ;
			$scope.nb_group_register_1_week = nb_group_register_1_week ;
			$scope.nb_group_register_1_month = nb_group_register_1_month ;
		}
	 
	 
	 $scope.count_nbr_event = function (data) {

			var i = 0;
			var nb_event_register_1_day = 0;
			var nb_event_register_1_week = 0;
			var nb_event_register_1_month = 0;
			
			angular.forEach(data.data,function(){
				if( compare(data.data[i].register_time,1) ) {
					nb_event_register_1_day ++;
				}
				if( compare(data.data[i].register_time,7) ) {
					nb_event_register_1_week ++;
				}
				if( compare(data.data[i].register_time,31) ) {
					nb_event_register_1_month ++;
				}
	            i++;
	        })
	     //   $scope.nbr_rows = i ;
			$scope.nb_event_register_1_day = nb_event_register_1_day ;
			$scope.nb_event_register_1_week = nb_event_register_1_week ;
			$scope.nb_event_register_1_month = nb_event_register_1_month ;
		}
	
}])